package com.demo.structure.array;

/**
 * 双向链表
 *
 * @author: 蔡奇峰
 * @date: 2020/7/12 21:07
 **/
public class MyDoubleLinkedList {

    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();

        Node node = new Node(14, "tale");
        Node student = new Node(18, "student");
        Node manager = new Node(18, "manager");

        doubleLinkedList.add(node);
        doubleLinkedList.add(student);

        System.out.println("查看链表");

        doubleLinkedList.list();
        System.out.println("修改：student");

        doubleLinkedList.update(manager);


        System.out.println("删除节点：student");

        doubleLinkedList.del(18);

        doubleLinkedList.list();

    }

}

class DoubleLinkedList {

    /**
     * 头节点
     */
    private Node head = new Node(0, "");

    public Node getHead() {
        return head;
    }

    /**
     * 添加节点
     * 和单链表相比多了指向前一个节点
     *
     * @param node
     */
    public void add(Node node) {
        // 辅助节点用于遍历链表
        Node temp = head;

        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }

        temp.next = node;

        node.pre = temp;
    }

    /**
     * 更新
     *
     * @param node
     */
    public void update(Node node) {
        // 链表是否为空
        if (isEmpty()) {
            System.out.println("链表为空！");
            return;
        }

        Node temp = head;

        while (true) {
            // 找不到则结束
            if (temp.next == null) {
                System.out.printf("更新失败！找不到 %d 节点", node.id);
                break;
            }

            if (node.id == temp.next.id) {
                temp.next.data = node.data;
                break;
            }
            // 后移
            temp = temp.next;
        }
    }

    /**
     * 删除节点
     *
     * @param id
     */
    public void del(int id) {
        // 链表是否为空
        if (isEmpty()) {
            System.out.println("链表为空！");
            return;
        }

        Node temp = head;
        while (true) {
            // 为空则找不到
            if (temp.next == null) {
                System.out.printf("删除失败！找不到 %d 节点！", id);
                break;
            }
            // 找到则结束
            if (id == temp.next.id) {
                temp.next.pre = temp;
                temp.next = temp.next.next;
                break;
            }

            // 后移
            temp = temp.next;
        }
    }

    /**
     * 链表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return head.next == null ? true : false;
    }

    /**
     * 查看链表
     */
    public void list() {
        // 链表是否为空
        if (isEmpty()) {
            System.out.println("链表为空！");
            return;
        }
        Node temp = head;

        while (true) {
            if (temp.next == null) {
                break;
            }
            System.out.println(temp.next.toString());
            temp = temp.next;
        }
    }

}

class Node {

    /**
     * 指向下一个节点
     */
    public Node next;
    /**
     * 指向前一个节点
     */
    public Node pre;

    public int id;
    /**
     * data域
     */
    public String data;

    public Node(int id, String data) {
        this.id = id;
        this.data = data;
    }

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", data='" + data + '\'' +
                '}';
    }
}